package com.common.mybatis.plugins;

import java.io.Serializable;

/**
 * 分页对象
 * @author guanghong.ji
 */
public class Pager implements Serializable {

	/**
	 * 分页实体 pageIndex 从 1 开始 ... 默认 每页30条
	 * 
	 */
	private static final long serialVersionUID = 7758990959529311865L;
	public static final int defaultPageSize = 10;
	private int pageIndex;
	/**
	 * 每页长度
	 */
	private int pageSize;

	public Pager() {
		pageIndex = 1;
		pageSize = defaultPageSize;
	}

	public Pager(int pageIndex, int pageSize) {
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
		
		if(this.pageIndex < 1){
			this.pageIndex = 1;
		}
	}

	public Pager nextPage() {
		++pageIndex;
		return this;
	}

	public Pager prevPage() {
		if (pageIndex > 1) {
			--pageIndex;
		}
		return this;
	}

	public Pager firstPage() {
		this.pageIndex = 1;
		return this;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		
		if(pageIndex >= 1){
			this.pageIndex = pageIndex;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		if(pageSize > 0)
		this.pageSize = pageSize;
	}

	public int getPageBegin() {
		if (pageIndex <= 1) {
			return 0;
		}
		return (pageIndex - 1) * pageSize;
	}

	public int getPageEnd() {

		return getPageBegin() + pageSize;
	}
}
